
package com.bangcommunity.bbframe.sdm.aop.handler;

import com.bangcommunity.bbframe.common.exception.ValidateUtil;
import com.bangcommunity.bbframe.sdm.aop.ApiCheckApp;
import com.bangcommunity.bbframe.sdm.aop.WrapAspectUtil;
import com.bangcommunity.bbframe.sdm.domain.ClientParam;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.Method;

/**
 * Created by tanghc on 17/1/18.
 */
public class ServiceApiCheckAppPreHandler extends PreHandler {
    @Override
    protected boolean doPreHandler(ProceedingJoinPoint pjp, Object[] args, Method method, String className) {

        ApiCheckApp annotation = WrapAspectUtil.findAnnotation(pjp.getTarget().getClass(), method, ApiCheckApp.class);
        if (null != annotation) {
            ClientParam clientParam = retriveUcParam(args);
            if (annotation.required()) {
                ValidateUtil.checkNotNull(clientParam, "参数不能为空");
            }
            if (null != clientParam) {
                ValidateUtil.checkNotEmpty(clientParam.getProductCode(), "productCode不能为空");
                ValidateUtil.checkNotEmpty(clientParam.getAppCode(), "appCode不能为空");
            }
        }
        return true;
    }

    protected ClientParam retriveUcParam(Object[] args) {
        if (null != args) {
            for (Object o : args) {
                if (o instanceof ClientParam) {
                    return (ClientParam) o;
                }
            }
        }
        return null;
    }
}
